package com.zlx.sharelive.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.sharelive.R;
import com.zlx.sharelive.activity.AuctionAc;
import com.zlx.sharelive.activity.MessageListAc;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 竞拍结果
 */
public class BiddingResultsFg extends BaseFg {

    @BindView(R.id.tv_type1)
    TextView tv_type1;
    @BindView(R.id.tv_type2)
    TextView tv_type2;
    @BindView(R.id.view_type1)
    View view_type1;
    @BindView(R.id.view_type2)
    View view_type2;

    private List<TextView> mTextViewList = new ArrayList<>();
    private List<View> mViewList = new ArrayList<>();
    private List<BaseFg> mFragment = new ArrayList<>();


    @Override
    protected int getLayoutId() {
        return R.layout.fg_bidding_results;
    }


    @Override
    protected void initViews() {
        super.initViews();

        mTextViewList.clear();
        mTextViewList.add(tv_type1);
        mTextViewList.add(tv_type2);
        mViewList.clear();
        mViewList.add(view_type1);
        mViewList.add(view_type2);

        initFragment();
    }

    private void initFragment() {
        mFragment.clear();
        mFragment.add(new MyBiddingFg());
        mFragment.add(new MyAuctionFg());
        commitAllowingStateLoss(0);
    }

    private void commitAllowingStateLoss(int position) {
        hideAllFragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        Fragment currentFragment = getChildFragmentManager().findFragmentByTag(position + "");
        if (currentFragment != null) {
            transaction.show(currentFragment);
        } else {
            currentFragment = mFragment.get(position);
            transaction.add(R.id.frameLayout, currentFragment, position + "");
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏所有Fragment
     */
    private void hideAllFragment() {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        for (int i = 0; i < mFragment.size(); i++) {
            Fragment currentFragment = getChildFragmentManager().findFragmentByTag(i + "");
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
        }
        transaction.commitAllowingStateLoss();
    }


    @OnClick({R.id.iv_back, R.id.ll_type1, R.id.ll_type2})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ((AuctionAc) getContext()).finish();
                break;
            case R.id.ll_type1:
                commitAllowingStateLoss(0);
                setTextViewColor(0);
                break;
            case R.id.ll_type2:
                commitAllowingStateLoss(1);
                setTextViewColor(1);
                break;
        }
    }


    public void setTextViewColor(int position) {
        for (int i = 0; i < mTextViewList.size(); i++) {
            if (position == i) {
                mTextViewList.get(position).setTextColor(getResources().getColor(R.color.hot_pink));
                mViewList.get(position).setVisibility(View.VISIBLE);
            } else {
                mTextViewList.get(i).setTextColor(getResources().getColor(R.color.black));
                mViewList.get(i).setVisibility(View.INVISIBLE);
            }
        }
    }
}
